/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerRecordConstant;
import net.dgg.dggcloud.service.jrcrm.constant.OrderConstant;
import net.dgg.dggcloud.service.jrcrm.dao.cms.CustomerRecordDao;
import net.dgg.dggcloud.service.jrcrm.entity.business.Business;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Customer;
import net.dgg.dggcloud.service.jrcrm.entity.customer.CustomerRecord;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.entity.customer.WhiteList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.dgg.dggcloud.service.common.base.service.BaseService;

import java.util.List;
import java.util.Map;


/**
 * @ClassName 〈CustomerRecordService>
 * @Description 客户备注service
 * @Author 朱永波
 * @Date 2017/11/6 10:24
 */
@Service
public class CustomerRecordService extends BaseService {

    @Autowired
    private CustomerRecordDao customerRecordDao;


    /**
     * 新增备注内容<br/>
     *
     * @param recordMap 参数map集合
     *                  cusId 客户id-不可为空
     *                  tableName 业务表名-不可为空
     *                  tableId 业务id-不可为空
     *                  recordType 备注类型-不可为空
     *                  operateUser 当前操作人id
     *                  content 备注内容
     *                  altJson json字符
     *                  ext1 扩展字段1
     *                  ext2 扩展字段2
     *                  ext3 扩展字段3
     *                  ext4 扩展字段4
     *                  ext5 扩展字段5
     * @return
     */
    public Long saveCustomerRecord(Map recordMap) {
        Object tableName = recordMap.get("tableName");
        Object tableId = recordMap.get("tableId");
        Object recordType = recordMap.get("recordType");
        Object operateUser = recordMap.get("operateUser");
        Object cusId = recordMap.get("cusId");
        Object newFollowerId = recordMap.get("newFollowerId");
        if (null == cusId) {
            throw new BizException("客户id不能为空", 103000);
        }
        if (null == tableName) {
            throw new BizException("业务表名不能为空", 103001);
        }
        if (null == tableId) {
            throw new BizException("业务id不能为空", 103002);
        }
        if (null == recordType) {
            throw new BizException("备注类型不能为空", 103003);
        }

        CustomerRecord cr = new CustomerRecord();
        cr.setId(keyWorker.nextId());
        if (null != operateUser) {
            User user = repository.get(User.class, Long.valueOf(operateUser.toString()));
            cr.setCreateUser(user);
            cr.setCreaterName(user.getName() +","+ user.getLoginName());
        }
        cr.setCustomerId(Long.valueOf(cusId.toString()));
        cr.setTableName(tableName.toString());
        cr.setTableId(Long.valueOf(tableId.toString()));
        cr.setRecordType(recordType.toString());
        if (null != recordMap.get("content")) {
            cr.setContent(recordMap.get("content").toString());
        }
        if (null != recordMap.get("fromUserId")) {
            cr.setFromUserId(Long.valueOf(recordMap.get("fromUserId").toString()));
        }
        if (null != recordMap.get("fromUserOrgId")) {
            cr.setFromUserOrgId(Long.valueOf(recordMap.get("fromUserOrgId").toString()));
        }
        if (null != recordMap.get("toUserId")) {
            cr.setToUserId(Long.valueOf(recordMap.get("toUserId").toString()));
        }
        if (null != recordMap.get("toUserOrgId")) {
            cr.setToUserOrgId(Long.valueOf(recordMap.get("toUserOrgId").toString()));
        }
        if (null != recordMap.get("altJson")) {
            cr.setAltJson(recordMap.get("altJson").toString());
        }
        if (null != recordMap.get("ext1")) {
            cr.setExt1(recordMap.get("ext1").toString());
        }
        if (null != recordMap.get("ext2")) {
            cr.setExt2(recordMap.get("ext2").toString());
        }
        if (null != recordMap.get("ext3")) {
            cr.setExt3(recordMap.get("ext3").toString());
        }
        if (null != recordMap.get("ext4")) {
            cr.setExt4(recordMap.get("ext4").toString());
        }
        if (null != recordMap.get("ext5")) {
            cr.setExt5(recordMap.get("ext5").toString());
        }
        repository.save(cr);
        return cr.getId();
    }


    /**
     * 修改备注内容<br/>
     *
     * @param
     *                   -不可为空
     *                  tableName 业务表名-不可为空
     *                  tableId 业务id-不可为空
     *                  recordType 备注类型-不可为空
     *                  operateUser 当前操作人id
     *                  content 备注内容
     *                  altJson json字符
     *                  ext1 扩展字段1
     *                  ext2 扩展字段2
     *                  ext3 扩展字段3
     *                  ext4 扩展字段4
     *                  ext5 扩展字段5
     * @return
     */
    public Long updateCustomerRecord(CustomerRecord cr) {
        repository.saveOrUpdate(cr);
        return cr.getId();
    }


    /**
     * 统计记录总数
     *
     * @param map 查询条件
     *            cusId 客户id
     *            tableName 业务表名
     *            tableId 业务id
     *            recordType 备注类型
     *            content 备注内容
     *            ext1 扩展字段1
     *            ext2 扩展字段2
     *            ext3 扩展字段3
     *            ext4 扩展字段4
     *            ext5 扩展字段5
     * @return
     */
    public Integer countCustomerRecordByMap(Map map) {
        if (!map.containsKey("recordTable")) {
            throw new BizException("请传入记录表名",10001);
        }
        return customerRecordDao.countCustomerRecordByMap(map);
    }

    /**
     * 查询记录列表
     *
     * @param map 查询条件
     *            customerId 客户id
     *            tableName 业务表名
     *            tableId 业务id
     *            recordType 备注类型
     *            content 备注内容
     *            ext1 扩展字段1
     *            ext2 扩展字段2
     *            ext3 扩展字段3
     *            ext4 扩展字段4
     *            ext5 扩展字段5
     * @return
     */
    public List<Map> listCustomerRecordByMap(Map map) {
        if (!map.containsKey("recordTable")) {
            throw new BizException("请传入记录表名",10001);
        }
        return customerRecordDao.listCustomerRecordByMap(map);
    }

    /**
     * 根据id查询备注记录
     *
     * @param remarkId 备注记录id
     * @return
     */
    public CustomerRecord getRecordById(Long remarkId) {
        return repository.get(CustomerRecord.class, remarkId);
    }

//    /**
//     * 保存操作记录
//     * @param recordMap 参数map集合
//     *                  cusId 客户id-不可为空 <br/>
//     *                  tableName 业务表名-不可为空 <br/>
//     *                  tableId 业务id-不可为空 <br/>
//     *                  recordType 备注类型-不可为空 <br/>
//     *                  operateUser 当前操作人id <br/>
//     *                  content 备注内容 <br/>
//     *                  altJson json字符 <br/>
//     *                  ext1 扩展字段1 <br/>
//     *                  ext2 扩展字段2 <br/>
//     *                  ext3 扩展字段3 <br/>
//     *                  ext4 扩展字段4 <br/>
//     *                  ext5 扩展字段5 <br/>
//     */
//    public void saveRecord(Map recordMap){
//        Object tableName = recordMap.get("tableName");
//        Object tableId = recordMap.get("tableId");
//        Object recordType = recordMap.get("recordType");
//        Object operateUser = recordMap.get("operateUser");
//        Object cusId = recordMap.get("cusId");
//        Object content = recordMap.get("content");
//        if(null == cusId){
//            throw new BizException("客户id不能为空",103000);
//        }
//        if(null == tableName){
//            throw new BizException("业务表名不能为空",103001);
//        }
//        if(null == tableId){
//            throw new BizException("业务id不能为空",103002);
//        }
//        if(null == recordType){
//            throw new BizException("备注类型不能为空",103003);
//        }
//        if(null == operateUser){
//            throw new BizException("操作人不能为空",103004);
//        }
//        if(null == content){
//            throw new BizException("备注内容不能为空",103004);
//        }
//        User user = repository.get(User.class,Long.valueOf(operateUser.toString()));
//        Map sqlMap = new HashMap();
//        StringBuilder sb = new StringBuilder();
//        Long id = keyWorker.nextId();
//        String now = DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
//        String month = DateUtil.dateToString(new Date(), "yyyyMM");
//        sb.append("INSERT INTO cus_record_").append(month).append(" (id, create_time, creater_id, creater_name, creater_org_id, customer_id, ");
//        sb.append("table_name, table_id, content, record_type, ");
//        if(null != recordMap.get("fromUserId")){
//            sb.append("from_user_id, ");
//        }
//        if(null != recordMap.get("fromUserOrgId")){
//            sb.append("from_user_org_id, ");
//        }
//        if(null != recordMap.get("toUserId")){
//            sb.append("to_user_id, ");
//        }
//        if(null != recordMap.get("toUserOrgId")){
//            sb.append("to_user_org_id, ");
//        }
//        if(null != recordMap.get("altJson")){
//            sb.append("alt_json, ");
//        }
//        if(null != recordMap.get("ext1")){
//            sb.append("ext_1, ");
//        }
//        if(null != recordMap.get("ext2")){
//            sb.append("ext_2, ");
//        }
//        if(null != recordMap.get("ext3")){
//            sb.append("ext_3, ");
//        }
//        if(null != recordMap.get("ext4")){
//            sb.append("ext_4, ");
//        }
//        if(null != recordMap.get("ext5")){
//            sb.append("ext_5, ");
//        }
//        sb.delete(sb.lastIndexOf(","),sb.length());
//        sb.append(") \n\t");
//        sb.append("VALUES (").append(id).append(", '").append(now).append("', ").append(user.getId()).append(", '");
//        sb.append(user.getName()).append(user.getLoginName()).append("',").append(user.getOrgId()).append(", ");
//        sb.append(Long.parseLong(cusId.toString())).append(", '").append(tableName.toString()).append("', ");
//        sb.append(tableId).append(", '").append(content.toString()).append("', '").append(recordType.toString()).append("', ");
//        if(null != recordMap.get("fromUserId")){
//            sb.append(Long.parseLong(recordMap.get("fromUserId").toString())).append(", ");
//        }
//        if(null != recordMap.get("fromUserOrgId")){
//            sb.append(Long.parseLong(recordMap.get("fromUserOrgId").toString())).append(", ");
//        }
//        if(null != recordMap.get("toUserId")){
//            sb.append(Long.parseLong(recordMap.get("toUserId").toString())).append(", ");
//        }
//        if(null != recordMap.get("toUserOrgId")){
//            sb.append(Long.parseLong(recordMap.get("toUserOrgId").toString())).append(", ");
//        }
//        if(null != recordMap.get("altJson")){
//            sb.append("'").append(recordMap.get("altJson").toString()).append("', ");
//        }
//        if(null != recordMap.get("ext1")){
//            sb.append("'").append(recordMap.get("ext1").toString()).append("', ");
//        }
//        if(null != recordMap.get("ext2")){
//            sb.append("'").append(recordMap.get("ext2").toString()).append("', ");
//        }
//        if(null != recordMap.get("ext3")){
//            sb.append("'").append(recordMap.get("ext3").toString()).append("', ");
//        }
//        if(null != recordMap.get("ext4")){
//            sb.append("'").append(recordMap.get("ext4").toString()).append("', ");
//        }
//        if(null != recordMap.get("ext5")){
//            sb.append("'").append(recordMap.get("ext5").toString()).append("', ");
//        }
//        sb.delete(sb.lastIndexOf(","),sb.length());
//        sb.append(")");
//        sqlMap.put("sql", sb.toString());
//        customerRecordDao.insertCustomerRecord(sqlMap);
//    }

    /**
     * 打电话时记录备注
     *
     * @param type       业务类型
     * @param customerId 客户id
     * @param tableId    业务id
     * @param user       操作人
     */
    public void saveCallCustomerRecord(Integer type, Long customerId, Long tableId, User user) {
        String tableName = "";
        switch (type){
            case 0:
                tableName = OrderConstant.ORF_ORDER;break;
            case 1:
                tableName = Business.TABLE_NAME;break;
            case 2:
                tableName = Customer.TABLE_NAME;break;
            case 4:
                tableName = WhiteList.TABLE_NAME;break;
            default:
                return;
        }
        CustomerRecord cr = new CustomerRecord();
        cr.setId(keyWorker.nextId());
        cr.setCreateUser(user);
        cr.setRecordType(CustomerRecordConstant.CUS_CALL_CUSTOMER);
        cr.setContent("拨打电话");
        cr.setTableId(tableId);
        cr.setTableName(tableName);
        cr.setCustomerId(customerId);
        repository.save(cr);
    }

}
